package com.kevin.struts2.view.converter;

import java.util.Map;
import java.util.StringTokenizer;

import org.apache.struts2.util.StrutsTypeConverter;

import com.kevin.struts2.dto.User;

public class UserConverter extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {

		User user = new User();

		String inputValue = values[0];

		StringTokenizer tokenizer = new StringTokenizer(inputValue, ";");
		user.setName(tokenizer.nextToken());
		user.setAge(Integer.valueOf(tokenizer.nextToken()));

		System.out.println("convertFromString");

		return user;
	}

	@Override
	public String convertToString(Map context, Object o) {

		User user = (User) o;

		System.out.println("convertToString");
		return user.getName() + ";" + user.getAge();
	}
}
